package customerEnter.Frame.JDBCDao.SqlProcessing.SqlServer;

import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameSqlProcessingInterface;
import customerEnter.Frame.JDBCDao.daoFrame.SqlDataBaseController;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;

/**
 * 类描述：處理 添加的語句
 * 
 * 类名称：DaoFrameSqlProcessingInsertImpl
 * 
 * 创建人：dy
 * 
 * 创建时间：2017-7-12 下午3:50:39
 * 
 */
public class DaoFrameSqlProcessingSelectPageImpl implements DaoFrameSqlProcessingInterface<String> {

	@Override
	public <T, K> String SqlProcessing(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		String order = daoFrameDMLBaseTransmission.getOrder();
		String descOrAsc = daoFrameDMLBaseTransmission.getDescOrAsc();

		int page = daoFrameDMLBaseTransmission.getPage();
		int size = daoFrameDMLBaseTransmission.getSize();

		String sql = "a." + order + " ";
		if (order.contains("NEWID()")) {
			sql = "  NEWID() ";
			descOrAsc = " ";
		}
		String SystemSele = SqlDataBaseController.getDMLSele();
		String SystemRule = SqlDataBaseController.getRule();
		String SystemWhere = SqlDataBaseController.getWhere();
		String Front = daoFrameDMLBaseTransmission.getFront();
		String ClassName = daoFrameDMLBaseTransmission.getTableName();
		String where = daoFrameDMLBaseTransmission.getWhere();

		String nativeSql = DaoFrameConntroller.SqlAcquire("", SystemSele, Front, SystemRule, ClassName, where);

		String countSql = DaoFrameConntroller.SqlAcquire("", SystemSele, "count(*)", SystemRule, ClassName, where);

		daoFrameDMLBaseTransmission.setFront(" TOP " + size + "  * " + ",(" + countSql + ") pagingParametersCount ");
		daoFrameDMLBaseTransmission.setTableName(" (SELECT ROW_NUMBER() OVER (ORDER BY   " + sql + "  " + descOrAsc
				+ " ) AS RowNumber,* FROM ( " + nativeSql + " ) a )   as A ");
		if (daoFrameDMLBaseTransmission.getPosition() == 0) {
			daoFrameDMLBaseTransmission.setWhere(SystemWhere + " and RowNumber > " + (size * page) + "");
		} else {
			daoFrameDMLBaseTransmission.setWhere(SystemWhere + " and RowNumber >( (" + SystemSele + " RowNumber"
					+ SystemRule + "  " + daoFrameDMLBaseTransmission.getTableName() + "  where id="
					+ daoFrameDMLBaseTransmission.getPosition() + " )+(" + (size * page) + ")) ");
		}

		DaoFrameConntroller.ManageDaoFramePameTransmission(daoFrameDMLBaseTransmission, daoFramePameTransmission);
		return daoFramePameTransmission.SQL;
	}

}
